<script setup lang="ts">
import type {
  FileConfigQo,
} from '#/system/file/fileConfig'
import {
  createFileConfigApi,
  getFileConfigDetailsApi,
  updateFileConfigApi,
} from '@/api/system/file/fileConfig'

interface OnCloseResolveType extends CloseResolveType {}
let onCloseResolve = function (_value: OnCloseResolveType) { }

const dialogRef = $ref<DialogInstance>()
const ruleFormRef = ref<FormInstance>()
let dialogModal = $ref<FileConfigQo>({})
let dialogAction = $ref<DialogAction>('add')
let confirmLoading = $ref(false)
let dialogLoading = $ref(false)

const rules = reactive<FormRules<FileConfigQo>>({
  name: [
    { required: true, message: '请输入配置名称', trigger: 'blur' },
  ],
  basePath: [
    { required: true, message: '请输入基础路径', trigger: 'blur' },
  ],
  endpoint: [
    { required: true, message: '请输入节点地址', trigger: 'blur' },
  ],
  accessKey: [
    { required: true, message: '请输入accessKey', trigger: 'blur' },
  ],
  accessSecret: [
    { required: true, message: '请输入accessSecret', trigger: 'blur' },
  ],
  domain: [
    { required: true, message: '请输入访问域名', trigger: 'blur' },
  ],
})
async function getDefaultModel() {
  return {
    storage: 'S3',
    master: 0,
  } as FileConfigQo
}
// 打开
async function showDialog(args: {
  title: string
  action?: DialogAction
  model: {
    id?: string
  }
}) {
  const dialogRes = dialogRef!.openDialog<OnCloseResolveType>({
    title: args.title,
  })

  ruleFormRef.value?.clearValidate()
  onCloseResolve = dialogRes.onCloseResolve

  dialogAction = args.action ?? 'add'

  if (args.model.id) {
    dialogModal.id = args.model.id
  }

  if (args.action === 'add') {
    const defaultModel = await getDefaultModel()
    dialogModal = {
      ...defaultModel,
      ...args.model,
    }
  }
  else if (args.action === 'edit' || args.action === 'views') {
    await reflash()
  }

  return await dialogRes.dialogPrm
}

async function reflash() {
  if (!dialogModal.id)
    return
  dialogLoading = true
  const res = await getFileConfigDetailsApi(dialogModal.id)
  if (!res)
    return
  dialogLoading = false
  Object.assign(dialogModal, res.data)
}

// 确认
async function confirm(formEl: FormInstance | undefined) {
  if (!formEl)
    return
  await formEl.validate(async (valid) => {
    if (!valid) {
      return
    }
    confirmLoading = true
    if (dialogAction === 'add') {
      await createFileConfigApi(dialogModal).finally(() => {
        confirmLoading = false
      })
    }
    else if (dialogAction === 'edit') {
      await updateFileConfigApi(dialogModal).finally(() => {
        confirmLoading = false
      })
    }
    confirmLoading = false
    Message({
      type: 'success',
      message: `${DialogActionMap[dialogAction as DialogAction]}成功`,
    })
    onCloseResolve({ type: true })
  })
}

// 取消
function cancel() {
  ruleFormRef.value?.clearValidate()
  onCloseResolve({ type: false })
}

defineExpose({ showDialog })
</script>

<template>
  <sy-dialog
    ref="dialogRef"
    :before-close="cancel"
    :hide-confirm="dialogAction === 'views'"
    :confirm-loading="confirmLoading"
    :disabled-confirm="dialogLoading"
    @confirm="confirm(ruleFormRef)"
  >
    <el-form
      ref="ruleFormRef"
      v-loading="dialogLoading"
      :model="dialogModal"
      :rules="rules"
      label-width="auto"
      status-icon
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="dialogModal.name"
          placeholder="请输入配置名称"
          :disabled="dialogAction === 'views'"
        />
      </el-form-item>
      <el-form-item v-if="dialogModal.storage === 'LOCAL'" label="基础路径" prop="basePath">
        <el-input
          v-model="dialogModal.basePath"
          placeholder="请输入基础路径"
          :disabled="dialogAction === 'views'"
        />
      </el-form-item>
      <el-form-item label="访问域名" prop="domain">
        <el-input
          v-model="dialogModal.domain"
          placeholder="请输入访问域名"
          :disabled="dialogAction === 'views'"
        />
      </el-form-item>
      <template v-if="dialogModal.storage !== 'LOCAL'">
        <el-form-item v-if="dialogModal.storage !== 'LOCAL'" label="节点地址" prop="endpoint">
          <el-input
            v-model="dialogModal.endpoint"
            placeholder="请输入节点地址"
            :disabled="dialogAction === 'views'"
          />
        </el-form-item>
        <el-form-item label="accessKey" prop="accessKey">
          <el-input
            v-model="dialogModal.accessKey"
            placeholder="请输入accessKey"
            :disabled="dialogAction === 'views'"
          />
        </el-form-item>
        <el-form-item label="secretKey" prop="accessSecret">
          <el-input
            v-model="dialogModal.accessSecret"
            type="password"
            show-password
            placeholder="请输入secretKey"
            :disabled="dialogAction === 'views'"
          />
        </el-form-item>
      </template>

      <el-form-item label="存储器" prop="storage">
        <sy-dict
          v-model="dialogModal.storage"
          type="segmented"
          dict-type="file_config_storage"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="dialogModal.remark"
          type="textarea"
          placeholder="请输入备注"
          :disabled="dialogAction === 'views'"
        />
      </el-form-item>
    </el-form>
  </sy-dialog>
</template>

<style scoped></style>
